/*
 * Created on 2005-10-31
 * Last modified on 2006-1-24
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.tools;
import java.util.*;

/**
 * 模拟数组
 * @author YeQiangWei by 我容易么我
 * Powered by YeQiangWei.com
 */
@SuppressWarnings("unused")
public class ArrayEdit{

    /**
     * 判断字符串txts模拟数组, 是否含有txt字符，f为分隔符
     * @param txts
     * @param txt
     * @param f
     * @return
     * com.yeqiangwei.tools
     */
    public static boolean txtsArray(String txts, String txt, String f){
        boolean bea = false;
        if(txts==null||txts.equals("")||txt==null||txt.equals("")){
            bea = false;
        }else{
            StringTokenizer st = new StringTokenizer(txts, f);
            String[] ishave;
            ishave = new String[st.countTokens()];
            for(int i=0;st.hasMoreTokens();i++){
                    ishave[i]=st.nextToken().trim();
                    if(ishave[i].equals(txt)){
                        bea = true;
                    }
            } 
        }      
        return bea;
    }
    
    /**
     * 判断字符串txts模拟数组, 是否含有txt字符，f为分隔符
     * @param txts
     * @param txt
     * @param f
     * @return
     * 2005-11-10 17:14:27 Made In YeQiangWei
     * com.yeqiangwei.tools
     */
    public static boolean unTxtsArray(String txts, String txt, String f){
        boolean bea = false;
        String str = "";
        if(txts==null||txts.equals("")||txt==null||txt.equals("")){
            bea = false;
        }else{
            StringTokenizer st = new StringTokenizer(txts, f);
            //String[] ishave;
            //ishave = new String[st.countTokens()];
            for(int i=0;st.hasMoreTokens();i++){
                str = st.nextToken().trim();
                if(txt.indexOf(str)>-1){
                    bea = true;
                }                
            } 
        }      
        return bea;
    }
    
    /**
     * txts类是数组的字符串, ii指定的某个数组位置， t字符串分割符
     * @param txts
     * @param ii
     * @param t
     * @return
     * 2005-11-10 17:14:37 Made In YeQiangWei
     * com.yeqiangwei.tools
     */
    public static boolean txtsArray(String txts, int ii, String t){
        if(!txts.equals("") && txts != null){
			StringTokenizer st = new StringTokenizer(txts, t);
			int[] gvcmgis;
			if(st.countTokens()<ii){ //判断指定数组长度是否越界
				gvcmgis = new int[ii];
				gvcmgis[ii-1]=0;
			}else{
				gvcmgis = new int[st.countTokens()];
			}
			for(int i=0;st.hasMoreTokens();i++){
				gvcmgis[i]=Integer.parseInt(st.nextToken().trim());
			}
		    /*
		     *由于数组下标都是从0开始，而习惯我们都是从1开始计算数组，所以下面
		     *ii自动减1，以中和语法和习惯上的矛盾
		     */
			if(gvcmgis[ii-1]==1){ 
				return true;
			}else{
				return false;
			}
        }
        else{
            return false;
        }
    }
    
    
    public static double txtsList(String txts, int ii, String t){
        double ret;
        if(!txts.equals("") && txts != null){
			StringTokenizer st = new StringTokenizer(txts, t);
			double[] txtslist; 
			if(st.countTokens()<ii){ //判断指定数组长度是否越界
				txtslist = new double[ii];
				txtslist[ii-1] = 0;
			}else{
				txtslist = new double[st.countTokens()];
			}
	        for(int i=0;st.hasMoreTokens();i++){
	        	try{
	        		txtslist[i] = Double.parseDouble(st.nextToken().trim());
	        	}catch(Exception e){
	        		txtslist[i]=0;
	        	}
            }
            ret = txtslist[ii-1];
        }else{
            ret = 0.0;
        }
        return ret;
    }
    
    
    /**
     * 将String数组转换成字符串，并以s作为分割符
     * @param txts
     * @param s
     * @return
     */
    public static String txtsList(String[] txts, String s){
    	if(txts==null){
    		return "";
    	}
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<txts.length;i++){
           sb.append(txts[i]);
           sb.append(s);
        }
        return sb.toString();
    }
    
    
    /**
     * 取出模拟数组制定位置的信息
     * @param txts
     * @param ii
     * @param t
     * @return
     * 2005-11-10 17:16:10 Made In YeQiangWei
     * com.yeqiangwei.tools
     */
    public static String getTextsInfo(String txts, int ii, String t){
        String str = "";
        if(txts==null||txts.equals("")||txts.equals(t)){
        	return "";
        }
        //if(txts.equals(t)){return "";}
        if(txts.substring(0,1).equals(t)){
        	txts = "~" + txts;
        }
        try{
	        if(!txts.equals("") && txts != null){
	        	txts = txts.replace((t+t),(t+"~"+t));
	        	//System.out.println(txts);
	            StringTokenizer st = new StringTokenizer(txts, t);
	            String[] txtslist; 
	            int ct = st.countTokens(); //4个元素可以循环四次
	            //System.out.println(ct);
	            if(ii>ct){ //判断指定数组长度是否越界
	                str = "";
	            }else{
	                txtslist = new String[ct];
	                for(int i=0;st.hasMoreTokens();i++){
		            	txtslist[i] = (st.nextToken());
		            	if(i==ii){
		            		str = txtslist[ii];
		            	}
		            	//System.out.println(txtslist[i]);
		            }
		            str = str.replace("~","");
	            }
	        }else{
	            str = "";
	        }
        }catch(Exception e){
        	str = "";
        	e.printStackTrace();
        }
        return str;
    }
    
   
    public static void main(String args[]){
    	System.out.println(ArrayEdit.getTextsInfo("|4",1,"|"));
    	System.out.println(ArrayEdit.getTextsInfo("aaa|",1,"|"));
    	System.out.println(ArrayEdit.getTextsInfo("|",1,"|"));
    	System.out.println(ArrayEdit.txtsArray("123|mf_314.swf| ","978701","|"));
    }
    
}
